//
//  MyController.m
//  Food
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyController.h"
#import "BaseHeader.h"
#import "Mycollection_ViewController.h"
#import "Mydownload_ViewController.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"
@interface MyController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *imageArray;
@property (nonatomic, retain) NSArray *nameArray;
@property (nonatomic, retain) UIImageView *header1;
@property (nonatomic, retain) UIImageView *header2;
@property (nonatomic, retain) UISwitch *swith;
@property (nonatomic, retain) UIImageView *nightImage;

@end

@implementation MyController

-(void)dealloc
{
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor= [UIColor colorWithRed:238 / 255.0 green:236 / 255.0 blue:232 / 255.0 alpha:1];
    self.navigationItem.title = @"我的";
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    
self.nameArray = @[@"我的收藏", @"清除缓存", @"夜间模式", @"关于我们"];
    [self getData];
    [self createTableView];
    
    self.nightImage = [[UIImageView alloc] initWithFrame:CGRectMake(- self.view.bounds.size.width, self.view.bounds.size.height * 36.73 / 100, self.view.bounds.size.width * 46.66 / 100, self.view.bounds.size.height * 26.23 / 100)];
    UIImage *nigImage = [UIImage imageNamed:@"yueliang1.png"];
    self.nightImage.image = nigImage;
    [self.view addSubview:self.nightImage];
    [_nightImage release];
}

- (void)getData
{
    self.imageArray = [NSMutableArray array];
    for (NSInteger i = 26; i < 30; i++) {
        NSString *path = [NSString stringWithFormat:@"%ld", i];
        UIImage *image = [UIImage imageNamed:path];
        [self.imageArray addObject:image];
    }
}

- (void)createTableView
{
    UIImageView *backView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backView.image = [UIImage imageNamed:@"backGround.jpg"];
    //    [self.view addSubview:backView];
    
    self.MytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.MytableView.delegate = self;
    self.MytableView.dataSource = self;
    
    self.MytableView.backgroundView = backView;
    
    self.header1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200.0 / 375 * ScreenWidth)];
    self.MytableView.tableHeaderView = _header1;
    [_header1 release];
    
    self.header2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200.0 / 375 * ScreenWidth)];
    self.header2.image = [UIImage imageNamed:@"我的背景"];
    [self.header1 addSubview:self.header2];
    [_header2 release];
    
    self.MytableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.MytableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.MytableView];
    [backView release];
    [_MytableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 2 ) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = self.nameArray[indexPath.row];
        cell.imageView.image = self.imageArray[indexPath.row];
        
        cell.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    

    if (indexPath.row == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        }
        cell.textLabel.text = self.nameArray[indexPath.row];
        cell.imageView.image = self.imageArray[indexPath.row];
        
        self.swith = [[UISwitch alloc] initWithFrame:CGRectMake(300.0 / 375 * ScreenWidth, 5, 80.0 / 375 *ScreenWidth, 30)];
        [cell.contentView addSubview:self.swith];
        [self.swith addTarget:self action:@selector(changeFun) forControlEvents:UIControlEventValueChanged];
        
        cell.backgroundColor = [UIColor clearColor];
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}


#warning 设置夜间模式
- (void) changeFun
{
    if (self.swith.on == YES) {
        NSString *str = @"0.618";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"str" object:str userInfo:@{@"name":@"NotificationCenter",@"type":@"type"}];
        [self change];
    }
    if (self.swith.on == NO) {
        NSString *str = @"1.0";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"str1" object:str userInfo:@{@"name1":@"NotificationCenter",@"type":@"type"}];
        [self change1];
    }
}

- (void) change
{
    [UIView animateWithDuration:0.6 animations:^{
        
        self.nightImage.frame = CGRectMake(self.view.bounds.size.width * 26.66 / 100, self.view.bounds.size.height * 36.73 / 100, self.view.bounds.size.width * 46.66 / 100, self.view.bounds.size.height * 26.23 / 100);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.nightImage.frame = CGRectMake(self.view.bounds.size.width, self.view.bounds.size.height * 36.73 / 100, self.view.bounds.size.width * 46.66 / 100, self.view.bounds.size.height * 26.23 / 100);
        }];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UIImage *image = [UIImage imageNamed:@"taiyang.png"];
        self.nightImage.image = image;
    });
}


- (void) change1
{
    [UIView animateWithDuration:0.6 animations:^{
        
        self.nightImage.frame = CGRectMake(self.view.bounds.size.width * 26.66 / 100, self.view.bounds.size.height * 36.73 / 100, self.view.bounds.size.width * 46.66 / 100, self.view.bounds.size.height * 26.23 / 100);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.nightImage.frame = CGRectMake(- self.view.bounds.size.width, self.view.bounds.size.height * 36.73 / 100, self.view.bounds.size.width * 46.66 / 100, self.view.bounds.size.height * 26.23 / 100);
        }];
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UIImage *image = [UIImage imageNamed:@"yueliang1.png"];
        self.nightImage.image = image;
    });
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imageArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0 / 375 *ScreenWidth ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        Mycollection_ViewController *MycollecVC = [[Mycollection_ViewController alloc] init];
        [self.navigationController pushViewController:MycollecVC animated:YES];
    }
    if (indexPath.row == 1) {
        NSInteger size = [[SDImageCache sharedImageCache] getSize];
        NSString *str = [NSString stringWithFormat:@"您成功清除缓存 %ldM", size / (1024 * 1024)];
        [[SDImageCache sharedImageCache] clearDisk];
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:nil  otherButtonTitles:@"确定", nil];
        [alterView show];
        [alterView release];
        [self.MytableView reloadData];
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yy = scrollView.contentOffset.y;
    if (yy <= 0) {
        yy = 0 - yy;
        self.header2.frame = CGRectMake(0 - yy, 0 - yy, self.view.frame.size.width + yy + yy, 200.0 / 375 * ScreenWidth + yy);
    }
}

#if 0
- (void)login
{
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
        }});
    
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
    }];
    
    
    [[UMSocialDataService defaultDataService] requestSnsFriends:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsFriends is %@",response.data);
    }];
    
    [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"response is %@",response);
    }];
    
}

#endif
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
